import { Matrix4, Vector2, Matrix3, Vector3, Vector4, Color } from "@nirvana/math";
import { WebGLCanvas } from "@nirvana/canvas";
import { Camera, ParticleSet } from "@nirvana/core";

import { ParticleSystem } from "@nirvana/particles";

async function main() {
  // 使用使用 Canvas
  let canvas = document.getElementById("engine-nirvana") as HTMLCanvasElement;
  const webCanvas = new WebGLCanvas(canvas, {});
  const engine = webCanvas.engine;
  const scene = engine.sceneManager.createScene("root");

  const camera = new Camera("root camera", engine);
  scene.addCamera(camera);
  camera.transform.setPosition(0, 0, 10);

  const gl = engine.gl;

  const psys = new ParticleSystem("smoke-1");
  psys.aspectratio = camera.aspect;
  psys.setParticleQuota(120);
  psys.setEmittedEmitterQuota(6);
  psys.defaultWidth = 0.05;
  psys.defaultHeight = 0.05;
  psys.isEmitting = true;
  // psys.material = "/dev-static/textures/clover.jpg";
  // psys.material = "/dev-static/textures/light.png";
  psys.material = "/dev-static/textures/snow.png";
  psys.stacks = 1;
  psys.slices = 1;

  const boxEmitter = psys.addEmitter("Box");
  boxEmitter.name = "fragment";
  // boxEmitter.emittedEmitter = "explosionnew1";
  boxEmitter.position.cloneFrom(new Vector3(-1.1, 1.6, 0));
  boxEmitter.enabled = true;
  boxEmitter.emitted = true;
  boxEmitter.setSize(4, 0, 1);
  boxEmitter.emissionRate = 8;
  boxEmitter.xRangeFactor = 1;
  boxEmitter.yRangeFactor = 1.0;
  boxEmitter.zRangeFactor = 0.0;
  boxEmitter.setDirection(new Vector3(0, -1, 0));
  boxEmitter.setMinTexcoordIndex(0);
  boxEmitter.setMaxTexcoordIndex(1);
  boxEmitter.setAngle(Math.PI / 4);
  boxEmitter.setParticleVelocity(0.1, 0.6);
  boxEmitter.setDuration(0.1, 1);
  boxEmitter.setTimeToLive(0.3, 10);
  boxEmitter.setRepeatDelay(0.5, 3.5);

  // const boxEmitter2 = psys.addEmitter("Box");
  // boxEmitter2.name = "fragment2";
  // boxEmitter2.position.x = 0.5;
  // boxEmitter2.enabled = true;
  // boxEmitter2.emitted = true;
  // boxEmitter2.setTimeToLive(0.17, 0.23);
  // boxEmitter2.setRepeatDelay(3.5, 3.5);
  // boxEmitter2.setSize(100, 100, 100);
  // boxEmitter2.emissionRate = 12;
  // boxEmitter2.xRangeFactor = 0.1;
  // boxEmitter2.yRangeFactor = 0.1;
  // boxEmitter2.zRangeFactor = 0.1;

  // const explosionnew1 = psys.addEmitter("Point");
  // explosionnew1.name = "explosionnew1";
  // explosionnew1.emissionRate = 12;
  // explosionnew1.setRepeatDelay(0.1, 0.5);
  // explosionnew1.setTimeToLive(0.3, 5);
  // // explosionnew1.setParticleVelocity(800, 800);
  // explosionnew1.setColourRangeStart(new Color(1, 1, 1, 1));
  // explosionnew1.setColourRangeEnd(new Color(1, 1, 1, 1));
  // explosionnew1.setAngle(360);
  // explosionnew1.setMinTexcoordIndex(0);
  // explosionnew1.setMaxTexcoordIndex(3);

  // const explosionnew2 = psys.addEmitter("Point");
  // explosionnew2.name = "explosionnew2";
  // explosionnew2.emissionRate = 100;
  // explosionnew2.setRepeatDelay(0.5, 0.5);
  // explosionnew2.setTimeToLive(0.4, 0.4);
  // explosionnew2.setParticleVelocity(800, 800);
  // explosionnew2.setColourRangeStart(new Color(1, 1, 1, 1));
  // explosionnew2.setColourRangeEnd(new Color(1, 1, 1, 1));
  // explosionnew2.setAngle(360);

  // const ringEmitter = psys.addEmitter("Cylinder");
  // ringEmitter.name = "Ellipsoid";
  // ringEmitter.xRangeFactor = 0.1;
  // ringEmitter.yRangeFactor = 0.1;
  // ringEmitter.zRangeFactor = 0.1;
  // ringEmitter.setSize(1, 1, 1);

  // const linearForceAffector = psys.addAffector("LinearForce");
  // linearForceAffector.mForceVector.y = -0.2;
  // // linearForceAffector.mForceVector.x = 0.1;
  // linearForceAffector.mForceApplication = "FA_AVERAGE";

  const scaleAffector = psys.addAffector("Scaler");
  scaleAffector.mScalAdj = -0.01;

  // const colourFaderAffector = psys.addAffector("ColourFader");
  // colourFaderAffector.setAdjust(1, 0, 0, 0.0);

  const colourInterpolator = psys.addAffector("ColourInterpolator");
  colourInterpolator.setTimeAdjust(0, 0);
  colourInterpolator.setTimeAdjust(1, 0.2);
  colourInterpolator.setTimeAdjust(2, 1);
  colourInterpolator.setColourAdjust(0, new Color(1, 1, 1, 0));
  colourInterpolator.setColourAdjust(1, new Color(1, 1, 1, 1));
  colourInterpolator.setColourAdjust(2, new Color(1, 1, 1, 0));

  const directionRandomiser = psys.addAffector("DirectionRandomiser");
  directionRandomiser.mRandomness = 10;
  directionRandomiser.mKeepVelocity = true;
  directionRandomiser.mScope = 0.001;

  const particleMesh = new ParticleSet(psys);
  scene.addEntity(particleMesh);

  // engine.update = () => {
  //   console.log("update");
  //   // psys.transform.
  // };
}

main();
